//var base = "http://127.0.0.1:8899/v1"
//var base = "http://120.76.41.18:8888/api/v1"
var authBase = "http://api.zhimagk.com/v1"
var base = "http://pq.zhimagk.com/v1"
//var base = "http://localhost:8899/v1"
// axios.interceptors.request.use(
// config => {
//     if (store.state.token) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
//         config.headers.Authorization = `Bearer ${store.state.token}`;
//     }
//     return config;
// },
// err => {
//     return Promise.reject(err);
// });
var http = {
	"base" : base,
	"authBase" : authBase,
	"get" : (vthis,url,params)=>{
		var url = http.base + "/" + url
		return vthis.$axios.get(url,{
			params : params || {},
			headers : {
				'Authorization' : 'Bearer ' + localStorage.getItem("access_token")
			}
		})
	},
	"authpost" : (vthis,url,params)=>{
		var url = http.authBase + "/" + url
		var postdata = new URLSearchParams();
		for(var i in params){
			postdata.append(i,params[i])
		}
		return vthis.$axios.post(url,postdata,{
			headers :{
				'Content-Type' : 'application/x-www-form-urlencoded',
				'Authorization' : 'Basic cHF0ZXN0Ol5wcTJAMTgk'
			}
		})
	},
	"post" : (vthis,url,params)=>{
		var url = http.base + "/" + url
		var postdata = new URLSearchParams();
		for(var i in params){
			postdata.append(i,params[i])
		}
		return vthis.$axios.post(url,postdata,{
			headers :{
				'Content-Type' : 'application/x-www-form-urlencoded',
				'Authorization' : 'Bearer ' + localStorage.getItem("access_token")
			}
		})
	},
	"put" : (vthis,url,params) => {
		var url = http.base + "/" + url
		var postdata = new URLSearchParams();
		for(var i in params){
			postdata.append(i,params[i])
		}
		return vthis.$axios.put(url,postdata,{
			headers :{
				'Content-Type' : 'application/x-www-form-urlencoded',
				'Authorization' : 'Bearer ' + localStorage.getItem("access_token")
			}
		})
	},
	"delete" : (vthis,url) => {
		var url = http.base + "/" + url
		// var postdata = new URLSearchParams();
		// for(var i in params){
		// 	postdata.append(i,params[i])
		// }
		return vthis.$axios.delete(url,{
			headers :{
				'Content-Type' : 'application/x-www-form-urlencoded',
				'Authorization' : 'Bearer ' + localStorage.getItem("access_token")
			}
		})
	},
	alive : function(vthis,callback,redirect){
		if(localStorage.getItem("access_token") == undefined){
			location.href = "#/login"
			if(redirect){
				vthis.$notify.error({
					title : "提示",
					message : "令牌已失效,请重新登录"
				})
			}
			return false
		}
		var url = http.base + "/admin_alive"
		var redirect = (redirect == undefined) ? true : redirect
		vthis.$axios.get(url,{
			headers : {
				'Authorization' : 'Bearer ' + localStorage.getItem("access_token")
			}
		}).then(function(res){
			var data = res.data
			if (data.code == "token_fail") {
				location.href = "#/login"
				if(redirect){
					vthis.$notify.error({
						title : "提示",
						message : "令牌已失效,请重新登录"
					})
				}
			}
			else{
				callback()
			}
		})
	}
}
export default http
